section .data
	http_header db "HTTP/1.1 200 OK",0x0d,0x0a,0x0d,0x0a,"Hello cSphere!",0x0d,0x0a,0x00

section .bss
	recvbuf: resb 8192
	
section .text
global  _start
_start:
	;Create socket
	mov rdi, 2
	mov rsi, 1
	mov rdx, 6
	mov rax, 41
	syscall
	mov rbx, rax

	;Bind to port 80
	mov rdx, 0x10         ; sizeof sockaddr_in
	push byte 0x00        ; INADDR_ANY
	push word 0x5000      ; 80
	push word 0x02        ; PF_INET
	mov rsi, rsp
	mov rdi, rbx
	mov rax, 0x31         ; sys_bind
	syscall
	
	;Start listening
	mov rdi, rbx
	mov rsi, 0;
	mov rax, 0x32
	syscall

	
	mov rbp, rsp
	sub rsp, 16
	
	mov QWORD [rbp-16], recvbuf

worker_thread_start:

	mov rdi, rbx
	xor rsi, rsi
	xor rdx, rdx
	mov rax, 0x2b
	syscall

	mov [rbp-8], rax ; save fd
	
	;HTTP Stuff starts here
	mov rdi, QWORD [rbp-8] ;fd
	mov rsi, [rbp-16]      ;buffer
	mov rdx, 8192    ;size
	
	mov rax, 0x2d
	syscall

	;remove the 0 handling

	mov rdi, [rbp-16]
	
	;create response
	mov rsi, http_header 
	mov rcx, 36
	cld
	rep movsb 
	
	mov rdi, [rbp-8]
	mov rsi, [rbp-16]
	
	mov rdx, 35
	
	;sys_send
	mov rax, 0x2c
	syscall
	
	;Close Socket
	mov rax, 3
	syscall
	
	jmp worker_thread_start 

